package com.zhuiYun.project.api.WxLogin.controller;

import com.zhuiYun.project.api.WxLogin.service.WxLoginService;
import com.zhuiYun.project.api.login.entity.User;
import com.zhuiYun.project.common.controller.BaseController;
import com.zhuiYun.project.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Description TODO 小程序微信授权登录
 * @ClassName WxLoginController
 * @Author HH
 * @Date 2023/4/10 19:03
 **/
@RestController
@RequestMapping("/wxLogin")
public class WxLoginController extends BaseController {

    @Autowired
    WxLoginService wxLoginService;

    /**
    * @Description 获取openid
    * @Author HH
    * @Date 2023/4/11 11:04
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param code
    */
    @PostMapping("/getOpenid")
    public CommonResult getOpenid(@RequestBody String code) throws IOException {
        return wxLoginService.getOpenid(code);
    }
    
    /**
    * @Description 根据openid获取手机号
    * @Author HH
    * @Date 2023/4/11 17:03
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param openid
    */
    @GetMapping("/getPhoneByOpenid")
    public CommonResult getPhoneByOpenid(String openid){
        return wxLoginService.getPhoneByOpenid(openid);
    }
    
    /**
     * @Description 小程序微信授权用户登录
     * @Author HH
     * @Date 2023/4/10 18:55
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param user
     */
    @PostMapping("/login")
    public CommonResult login(@RequestBody User user) {
        return wxLoginService.login(user);
    }

    /**
     * @Description 根据openid获取小程序用户信息
     * @Author HH
     * @Date 2023/4/10 18:47
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param openid
     */
    @GetMapping("/getUserInfo")
    public CommonResult getUserInfo(String openid){
        return wxLoginService.getUserInfo(openid);
    }

    /**
     * @Description 腾讯云发送手机短信
     * @Author HH
     * @Date 2023/4/12 9:43
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param phone
     */
    @GetMapping("/sendPhoneCode")
    public CommonResult sendPhoneCode(String phone){
        return wxLoginService.sendPhoneCode(phone);
    }

    /**
    * @Description 判断手机短信验证码是否正确
    * @Author HH
    * @Date 2023/4/12 13:55
    * @Return com.zhuiYun.project.common.response.CommonResult

    */
    @GetMapping("/verificationCode")
    public CommonResult verificationCode(String phone, String code){
        return wxLoginService.verificationCode(phone, code);
    }
}
